<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Type Conversion Functions | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'sql-functions-type-conversion.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/sql-functions-type-conversion.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/sql-functions-type-conversion.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/sql-functions-type-conversion.html" rel="nofollow" target="_blank">../en/sql-functions-type-conversion.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-sql.html">SQL access</a></span>
»
<span class="breadcrumb-link"><a href="sql-functions.html">Functions and Operators</a></span>
»
<span class="breadcrumb-node">Type Conversion Functions</span>
</div>
<div class="navheader">
<span class="prev">
<a href="sql-functions-string.html">« String Functions</a>
</span>
<span class="next">
<a href="sql-functions-geo.html">Geo Functions »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="sql-functions-type-conversion"></a>Type Conversion Functions<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/type-conversion.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>Functions for converting an expression of one data type to another.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="sql-functions-type-conversion-cast"></a><code class="literal">CAST</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/type-conversion.asciidoc">edit</a>
</h3>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">CAST(
    expression <a id="CO162-1"></a><i class="conum" data-value="1"></i>
 AS data_type) <a id="CO162-2"></a><i class="conum" data-value="2"></i></pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO162-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Expression to cast</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO162-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Target data type to cast to</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Description</strong></span>: Casts the result of the given expression to the target <a class="xref" href="sql-data-types.html" title="Data Types">data type</a>.
If the cast is not possible (for example because of target type is too narrow or because
the value itself cannot be converted), the query fails.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT CAST('123' AS INT) AS int;

      int
---------------
123</pre>
</div>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT CAST(123 AS VARCHAR) AS string;

    string
---------------
123</pre>
</div>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT YEAR(CAST('2018-05-19T11:23:45Z' AS TIMESTAMP)) AS year;

     year
---------------
2018</pre>
</div>
<div class="important admon">
<div class="icon"></div>
<div class="admon_content">
<p>Both ANSI SQL and Elasticsearch SQL types are supported with the former taking
precedence. This only affects <code class="literal">FLOAT</code> which due naming conflict, is interpreted as ANSI SQL
and thus maps to <code class="literal">double</code> in Elasticsearch as oppose to <code class="literal">float</code>.
To obtain an Elasticsearch <code class="literal">float</code>, perform casting to its SQL equivalent, <code class="literal">real</code> type.</p>
</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="sql-functions-type-conversion-convert"></a><code class="literal">CONVERT</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/sql/functions/type-conversion.asciidoc">edit</a>
</h3>
</div></div></div>
<p><strong>Synopsis:</strong></p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">CONVERT(
    expression, <a id="CO163-1"></a><i class="conum" data-value="1"></i>
    data_type)  <a id="CO163-2"></a><i class="conum" data-value="2"></i></pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO163-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Expression to convert</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO163-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Target data type to convert to</p>
</td>
</tr>
</table>
</div>
<p><span class="strong strong"><strong>Description</strong></span>: Works exactly like <a class="xref" href="sql-functions-type-conversion.html#sql-functions-type-conversion-cast" title="CAST"><code class="literal">CAST</code></a> with slightly different syntax.
Moreover, apart from the standard <a class="xref" href="sql-data-types.html" title="Data Types">data types</a> it supports the corresponding
<a href="https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/explicit-data-type-conversion-function?view=sql-server-2017" class="ulink" target="_top">ODBC data types</a>.</p>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT CONVERT('123', SQL_INTEGER) AS int;

      int
---------------
123</pre>
</div>
<div class="pre_wrapper lang-sql">
<pre class="programlisting prettyprint lang-sql">SELECT CONVERT('123', INTEGER) AS int;

      int
---------------
123</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="sql-functions-string.html">« String Functions</a>
</span>
<span class="next">
<a href="sql-functions-geo.html">Geo Functions »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>